﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using Wjtour.Common;

namespace Wjtour.BLL
{
    public class scenehotel_bll : base_dll
    {
        /// <summary>
        /// 检查景区是否存在该酒店
        /// </summary>
        /// <param name="sceneid"></param>
        /// <param name="hotelid"></param>
        /// <returns></returns>
        public bool Check(int sceneid, int hotelid)
        {
            string sql = "select * from scenehotel where sceneid=" + sceneid + " and hotelid=" + hotelid;
            return DbAccess.GetObjects<Wjtour.Domain.scenehotel>(sql).Count() > 0;
        }

        /// <summary>
        /// 获取某个景区的酒店
        /// </summary>
        /// <param name="sceneid"></param>
        /// <param name="page"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public PaginationData<Wjtour.Domain.hotel> GetList(int sceneid, int page, int pagesize = 20)
        {
            string where = " where 1=1";
            if (sceneid > 0)
                where += " and b.id = " + sceneid;

            string sql = string.Format(@"select c.* from scenehotel a
            inner join scene b on a.sceneid = b.id
            inner join hotel c on a.hotelid= c.id
                {0}
                order by a.id desc", where);
            PaginationData<Wjtour.Domain.hotel> data = GetPaginationData<Wjtour.Domain.hotel>(sql, page, pagesize);
            return data;
        }

        /// <summary>
        /// 获取景点酒店
        /// </summary>
        /// <param name="scenename"></param>
        /// <param name="page"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public PaginationData<Wjtour.Domain.dto.hotel_scene> GetList(string scenename, int page, int pagesize = 20)
        {
            string where = " where 1=1";
            if (!string.IsNullOrEmpty(scenename))
                where += " and b.scenename like '%" + Utility.GetSafeSql(scenename) + "%'";

            string sql = string.Format(@"select c.*,a.sceneid,b.scenename,a.id scenehotelid from scenehotel a
            inner join scene b on a.sceneid = b.id
            inner join hotel c on a.hotelid= c.id
                {0}
                order by a.id desc", where);
            PaginationData<Wjtour.Domain.dto.hotel_scene> data = GetPaginationData<Wjtour.Domain.dto.hotel_scene>(sql, page, pagesize);
            return data;
        }

        public Wjtour.Domain.scenehotel Load(int id)
        {
            string sql = "select * from scenehotel where id=" + id;
            return DbAccess.GetObjects<Wjtour.Domain.scenehotel>(sql).FirstOrDefault();
        }

        public bool Save(Wjtour.Domain.scenehotel entity)
        {
            string sql = "";

            DbCommand cmd = DbAccess.CreateCommand();

            if (entity.id > 0)
            {
                sql = @"UPDATE scenehotel SET 
                sceneid = @sceneid,
                hotelid = @hotelid,
                addtime = @addtime
                 WHERE id=@id";
                DbAccess.AddInParameter(cmd, "@id", DbType.Int32, entity.id);

            }
            else
            {
                sql = @"INSERT INTO scenehotel( 
                sceneid,hotelid,addtime)VALUES(
                @sceneid,@hotelid,@addtime)";
            }
            DbAccess.AddInParameter(cmd, "@sceneid", DbType.Int32, entity.sceneid);
            DbAccess.AddInParameter(cmd, "@hotelid", DbType.Int32, entity.hotelid);
            DbAccess.AddInParameter(cmd, "@addtime", DbType.DateTime, entity.addtime);
            cmd.CommandText = sql;
            cmd.CommandType = System.Data.CommandType.Text;
            return DbAccess.ExecuteNonQuery(cmd) > 0;
        }

        public bool Save(Wjtour.Domain.hotel entity, int sceneid)
        {
            string sql = "user_AddSceneHotel";

            DbCommand cmd = DbAccess.CreateCommand();
            DbAccess.AddInParameter(cmd, "@sceneid", DbType.Int32, sceneid);
            DbAccess.AddInParameter(cmd, "@hotelid", DbType.Int32, entity.id);
            DbAccess.AddInParameter(cmd, "@hotelname", DbType.String, entity.hotelname);
            DbAccess.AddInParameter(cmd, "@address", DbType.String, entity.address);
            DbAccess.AddInParameter(cmd, "@opentime", DbType.String, entity.opentime);
            DbAccess.AddInParameter(cmd, "@decrocatetime", DbType.String, entity.decrocatetime);
            DbAccess.AddInParameter(cmd, "@reservephone", DbType.String, entity.reservephone);
            DbAccess.AddInParameter(cmd, "@averageprice", DbType.Decimal, entity.averageprice);
            DbAccess.AddInParameter(cmd, "@hotelstar", DbType.Int32, entity.hotelstar);
            DbAccess.AddInParameter(cmd, "@trafficguide", DbType.String, entity.trafficguide);
            DbAccess.AddInParameter(cmd, "@introduce", DbType.String, entity.introduce);
            DbAccess.AddInParameter(cmd, "@img", DbType.String, entity.img);
            DbAccess.AddInParameter(cmd, "@addtime", DbType.DateTime, entity.addtime);
            cmd.CommandText = sql;
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            return DbAccess.ExecuteNonQuery(cmd) > 0;

        }

        public int BatchDelete(string ids)
        {
            string sql = "delete from scenehotel where id in(" + ids + ")";
            return DbAccess.ExecuteNonQuery(sql);
        }

        public int Delete(int sceneid, int hotelid)
        {
            string sql = "delete from scenehotel where sceneid =" + sceneid + " and hotelid=" + hotelid;
            return DbAccess.ExecuteNonQuery(sql);
        }
    }
}
